package com.huawei.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huawei.crm.domain.Permission;
import com.huawei.crm.mapper.PermissionMapper;
import com.huawei.crm.query.QueryObject;
import com.huawei.crm.service.IPermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionServiceImpl extends AbstractService<PermissionMapper, Permission, Long>
        implements IPermissionService {

    @Autowired
    private PermissionMapper mapper;


    @Override
    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }


    @Override
    @Transactional(readOnly = true)
    public List<Permission> listAll() {
        return mapper.selectAll();
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo<Permission> query(QueryObject qo) {
        // 告诉mybatis需要去哪里拼接sql
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    // 获取spring容器对象
    @Autowired
    private ApplicationContext ctx;

    // 加载权限
    @Override
    public void reload() {
        // 先查询表中所有的权限表达式
        List<String> expressions = mapper.selectAllExpressions();

        // 从容器中获取所有的controller对象
        // key：bean的名称, value:bean的对象
        Map<String, Object> beansWithAnnotation = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> collections = beansWithAnnotation.values();
        // 获取每个controller中的方法
        for (Object collection : collections) {
            Method[] methods = collection.getClass().getSuperclass().getDeclaredMethods();
            System.out.println(Arrays.asList(methods));
            // 获取方法上贴的注解
            for (Method method : methods) {
                // 判断方法上是否有指定的权限注解:RequiredsRermissions
                if (method.isAnnotationPresent(RequiresPermissions.class)) {
                    // 获取方法上贴的注解
                    RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                    // 获取注解中传递的参数
                    String[] value = annotation.value();
                    String name = value[0]; // 权限名称
                    String expression = value[1]; // 权限表达式
                    System.out.println(name);
                    System.out.println(expression);
                    // 将参数传到数据库中
                    // 先判断表达是否在数据库中,如果没有再执行保存操作
                    if (!expressions.contains(expression)) {
                        Permission p = new Permission();
                        p.setName(name);
                        p.setExpression(expression);
                        mapper.insert(p);
                    }
                }
            }

        }

    }

}
